/**
 * Created by lcfevr on 16/7/18.
 */
import Config from '../config/config';
import * as Cache from './cache';
import cacheKeys from './cache-keys';
export function strLength(str) {
    var oLength = 0;
    for (let ch of str) {
        if (ch.codePointAt(0) > 0xFFF) {
            oLength += 4
        } else {
            oLength += 2
        }
    }
    return oLength;
}
export function getLength(str) {
    return [...str].length;
}

// 环境
export const env = {
  isIPad: /ipad/i.test(window.navigator.userAgent),
  isIphone: /iphone|ipad|ipod/i.test(window.navigator.userAgent),
  isWechat: /MicroMessenger/i.test(window.navigator.userAgent)
};

// 判断
export const is = {
  has(val) {
    return !!val;
  },
  not(val) {
    return !val;
  },
  empty(val) {
    return val === '';
  },
  equal(v1, v2) {
    return v1 === v2;
  },
  qq(val) {
    return val &&　/^[1-9]\d{4,12}$/.test(val);
  },
  name(val) {
    return val &&　/^[A-Za-z0-9\u4E00-\u9FA5_]{2,14}$/.test(val);
  },
  mobile(val) {
    return val && /(^(13\d|14[57]|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/.test(val);
  },
  fifteen(val) {
    return val && /^[\d\-_+]{2,15}$/.test(val);
  },
  email(val) {
    return val && /^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(val);
  },
  password(val) {
    return val && val.length && val.length > 5 && !/^\d+$/.test(val);
  }
};

export function getMultiArr(arr, ...newArr) {
    arr.push(...newArr);
    return arr;
}
export function pushArr(arr, newArr) {
    arr.push(...newArr)
    return arr;
}
export function baseImg(imgObj, callback) {
    var reader = new FileReader();
    reader.onload = callback()
    reader.readAsDataURL(imgObj)
}
export function objToArr(obj) {
    var arr = [];
    Object.keys(obj).forEach(function (item) {
        arr.push(obj[item])
    })

    return arr;
}
export function setPageTitle(title) {
    document.title = title;

    if (Config.isIphone && Config.isWechat) {
        var body = document.getElementsByTagName('body')[0];
        var iframe = document.createElement('iframe');
        iframe.setAttribute('src', '/favicon.ico');

        iframe.addEventListener('load', function () {
            setTimeout(function () {
                iframe.removeEventListener('load');
                document.body.removeChild(iframe);
            }, 0);
        });
        document.body.appendChild(iframe);
    }

}
export function loadJs(url, callback) {


}

export function camelcaseToHyphen(str) {
    return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}


export function findComponentUpward (content, componentName, componentNames) {
    if (typeof componentName === 'string') {
        componentNames = [componentName];
    } else {
        componentNames = componentName;
    }

    let parent = content.$parent;
    let name = parent.$options.name;
    while (parent && (!name || componentNames.indexOf(name) < 0)) {
        parent = parent.$parent;
        if (parent) name = parent.$options.name;
    }
    return parent;
}

export function findComponentDownward (content, componentName) {
    const childrens = content.$children;
    let children = null;

    if (childrens.length) {
        childrens.forEach(child => {
            const name = child.$options.name;
            if (name === componentName) {
                children = child;
            }
        });

        for (let i = 0; i < childrens.length; i++) {
            const child = childrens[i];
            const name = child.$options.name;
            if (name === componentName) {
                children = child;
                break;
            } else {
                children = findComponentDownward(child, componentName);
                if (children) break;
            }
        }
    }
    return children;
}

export function findComponentsDownward (content, componentName, components = []) {
    const childrens = content.$children;

    if (childrens.length) {
        childrens.forEach(child => {
            const name = child.$options.name;
            const childs = child.$children;

            if (name === componentName) components.push(child);
            if (childs.length) {
                const findChilds = findComponentsDownward(child, componentName, components);
                if (findChilds) components.concat(findChilds);
            }
        });
    }
    return components;
}

export function random_str() {
    let len = 32;
    let chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    let max = chars.length;

    let str = '';
    for (let i = 0; i < len; i++) {
        str += chars.charAt(Math.floor(Math.random() * max));
    }
    return str;
}

// 获取查询参数
export function query(path){
  let obj={};
  let str=path.slice(path.indexOf("?")+1);
  let arr=str.split("&");
  for(var i in arr){
    var strm=arr[i];
    obj[strm.slice(0,strm.indexOf("="))]=strm.slice(strm.indexOf("=")+1);
  }
  return obj;
}
var cache_keys =cacheKeys(Config.BRAND.cache);
export {
  cache_keys,
  Cache
}
